/**
 * @author QinGuan
 * @date:2016-1-25 下午02:27:24
 * @version :3.0
 *
 */
package com.ctvit.sms.platform.service;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import com.ctvit.framework.dao.Condition;
import com.ctvit.framework.service.BaseService;
import com.ctvit.sms.platform.domain.Contentlog;
import com.ctvit.wsservice.basedata.domain.*;


import org.apache.commons.lang3.StringUtils;

@SuppressWarnings("serial")
public class ContentLogService extends BaseService {
	public List<Map<String, ?>> result = null;// 返回的结果集合

	public Boolean checkProgInZonelist(String chanlCode,String progCode) {
		if (StringUtils.isEmpty(chanlCode)) {
			return null;
		} else if (StringUtils.isEmpty(progCode)) {
			return null;
		}       //TODO 由于是string 所以需要过滤特殊字符

		Condition condition = new Condition();
		condition.add("chanlCd", chanlCode);
		condition.add("procode", progCode);

		List<Channelzone> result = genericDao.getByCondition(Channelzone.class, condition, null);

		if (result.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}
	public List<SearchProgList> getPageProgList(Map conditionMap,int pageNo , int pageSize) {
		return genericDao.queryBySqlIDPage("basedata.custom.getSearchProgListPage", conditionMap, pageNo, pageSize);
	}
	public String  getSearchProgListSize(Map conditionMap) {
		List<String> lstList=genericDao.queryBySqlID("basedata.custom.getSearchProgListCnt", conditionMap);
		return (String)lstList.get(0);
	}
	public List<SearchProgList> getFileProgList(Map conditionMap,int pageNo , int pageSize) {
		return genericDao.queryBySqlIDPage("basedata.custom.getFileProgListPage", conditionMap, pageNo, pageSize);
	}
	public String  getFileProgListSize(Map conditionMap) {
		List<String> lstList=genericDao.queryBySqlID("basedata.custom.getFileProgListCnt", conditionMap);
		return (String)lstList.get(0);
	}

	/**
	 * 在节目单的最后插入节目
	 *
	 * @param proItemCd
	 * @param chanlCd
	 * @param userId
	 */
	public void insertChannelzone(String proItemCd, String chanlCd, String userId) {
		StringBuffer str_sql = new StringBuffer();
		str_sql.append("insert into t_channelzone(f_chanl_cd,f_order,f_subj_cd,f_procode,f_subj_length,f_pgnam,f_crtuser,f_aud_Ver,f_cap_ver,f_lastmodifytime)\n" +
				"(SELECT \n" +
				"    '" + chanlCd + "',\n" +
				"    ifnull((select max(f_order) + 1 from t_channelzone where f_chanl_cd = '" + chanlCd + "' and f_crtuser = '" + userId +
				"'),1) as f_order,\n" +
				"    plist.Z_PLAY_SUBJ_CD as z_subj_cd,\n" +
				"    plist.Z_PROCODE,\n" +
				"    plist.z_subj_time as f_subj_length,\n" +
				"    plist.Z_PGNAM,\n" +
				"    '" + userId + "',\n" +
				"    plist.Z_AUD_VER,\n" +
				"    plist.Z_CAP_VER,\n" +
				"    now()\n" +
				"FROM\n" +
				"    zcdm_t_proglist AS plist\n" +
				"WHERE\n" +
				"    plist.z_pro_item_cd = '" + proItemCd + "'" + " )");
		genericDao.updateBySqlStr(str_sql.toString());
	}
	/**
	 * 在节目单的最后插入节目
	 *
	 * @param proItemCd
	 * @param chanlCd
	 * @param userId
	 */
	public void insertChannelzoneByProCode(String proCode, String chanlCd, String userId) {
		StringBuffer str_sql = new StringBuffer();
		str_sql.append("insert into t_channelzone(f_chanl_cd,f_order,f_subj_cd,f_procode,f_subj_length,f_pgnam,f_crtuser,f_aud_Ver,f_cap_ver,f_lastmodifytime)\n" +
				"(SELECT \n" +
				"    '" + chanlCd + "',\n" +
				"    ifnull((select max(f_order) + 1 from t_channelzone where f_chanl_cd = '" + chanlCd + "' and f_crtuser = '" + userId +
				"'),1) as f_order,\n" +
				"    plist.z_subj_cd as z_subj_cd,\n" +
				"    plist.Z_PROCODE,\n" +
				"    plist.z_time_estm as f_subj_length,\n" +
				"    plist.Z_PGNAM,\n" +
				"    '" + userId + "',\n" +
				"    plist.Z_AUD_VER,\n" +
				"    plist.Z_CAP_VER,\n" +
				"    now()\n" +
				"FROM\n" +
				"    zppm_t_proginfo AS plist\n" +
				"WHERE\n" +
				"    plist.z_procode = '" + proCode + "'" + " )");
		genericDao.updateBySqlStr(str_sql.toString());
	}

	/**
	 * 在order的位置插入节目
	 *
	 * @param proItemCd
	 * @param chanlCd
	 * @param userId
	 * @param order
	 */
	public int insertChannelzone(String proItemCd, String chanlCd, String userId, Integer order) {
		if (order == null || StringUtils.isEmpty(chanlCd)) {
			return -1;
		}
		//先把当前order之后 包括order的条目 order+1
		String sqlString;
		sqlString = "update t_channelzone set f_order=f_order+1 where f_chanl_cd='" + chanlCd + "' and f_order>=" + order;
		genericDao.updateBySqlStr(sqlString);
		//然后再把新条目的order设置为传入的值  插入
		StringBuffer str_sql = new StringBuffer();
		str_sql.append("insert into t_channelzone(f_chanl_cd,f_order,f_subj_cd,f_procode,f_subj_length,f_pgnam,f_crtuser,f_aud_Ver,f_cap_ver,f_lastmodifytime)\n" +
				"(SELECT \n" +
				"    '" + chanlCd + "',\n" +
				+   order+",\n" +
				"    plist.Z_PLAY_SUBJ_CD as z_subj_cd,\n" +
				"    plist.Z_PROCODE,\n" +
				"    plist.z_subj_time as f_subj_length,\n" +
				"    plist.Z_PGNAM,\n" +
				"    '" + userId + "',\n" +
				"    plist.Z_AUD_VER,\n" +
				"    plist.Z_CAP_VER,\n" +
				"    now()\n" +
				"FROM\n" +
				"    zcdm_t_proglist AS plist\n" +
				"WHERE\n" +
				"    plist.z_pro_item_cd = '" + proItemCd + "'" + " )");
		genericDao.updateBySqlStr(str_sql.toString());
		return 1;
	}
	/**
	 * 在order的位置插入节目
	 *
	 * @param proItemCd
	 * @param chanlCd
	 * @param userId
	 * @param order
	 */
	public int insertChannelzoneByProCode(String proCode, String chanlCd, String userId, Integer order) {
		if (order == null || StringUtils.isEmpty(chanlCd)) {
			return -1;
		}
		//先把当前order之后 包括order的条目 order+1
		String sqlString;
		sqlString = "update t_channelzone set f_order=f_order+1 where f_chanl_cd='" + chanlCd + "' and f_order>=" + order;
		genericDao.updateBySqlStr(sqlString);
		//然后再把新条目的order设置为传入的值  插入
		StringBuffer str_sql = new StringBuffer();
		str_sql.append("insert into t_channelzone(f_chanl_cd,f_order,f_subj_cd,f_procode,f_subj_length,f_pgnam,f_crtuser,f_aud_Ver,f_cap_ver,f_lastmodifytime)\n" +
				"(SELECT \n" +
				"    '" + chanlCd + "',\n" +
				+   order+",\n" +
				"    plist.Z_SUBJ_CD as z_subj_cd,\n" +
				"    plist.Z_PROCODE,\n" +
				"    plist.z_time_estm as f_subj_length,\n" +
				"    plist.Z_PGNAM,\n" +
				"    '" + userId + "',\n" +
				"    plist.Z_AUD_VER,\n" +
				"    plist.Z_CAP_VER,\n" +
				"    now()\n" +
				"FROM\n" +
				"    zppm_t_proginfo AS plist\n" +
				"WHERE\n" +
				"    plist.z_procode = '" + proCode + "'" + " )");
		genericDao.updateBySqlStr(str_sql.toString());
		return 1;
	}
	public ZcdmTProglist getProugListItemByPk(String pk) {
		if (StringUtils.isEmpty(pk)) {
			return null;
		}
		Condition condition = new Condition();
		condition.add("zProItemCd", pk);

		List<ZcdmTProglist> result = genericDao.getByCondition(ZcdmTProglist.class, condition, null);
		if (result.isEmpty()) {
			return null;
		} else {
			return result.get(0);
		}
	}
	public List<ZcdmTChanl> getChanls() {
		return genericDao.getByCondition(ZcdmTChanl.class, null, null);
	}
	public List<VChanl> getMyChanls(String userId) {
		List<VChanl> list1=genericDao.getByCondition(VChanl.class, null, null);
		List<VChanl> myChanls=new ArrayList();
		Editorconfig editorconfig=genericDao.getObjectByCondition(Editorconfig.class, new Condition().add("userId",userId));
		if (editorconfig!=null){
			for (VChanl zcdmTChanl : list1) {
				if (editorconfig.getEditChannels().indexOf(zcdmTChanl.getZChanlCd())>=0){
					myChanls.add(zcdmTChanl);
				}
			}
		}
		return myChanls;
	}
	public int updateProgInfo(String chanlZoneId,ZppmTProginfo zppmTProginfo,String[] filter){
		Condition condition = new Condition();
		condition.add("zProcode", zppmTProginfo.getZProcode());
		condition.add("zAudVer", zppmTProginfo.getZAudVer());
		condition.add("zCapVer", zppmTProginfo.getZCapVer());

		int n = genericDao.updateByCondition(zppmTProginfo, filter, condition);
		if (n == 0) {
			String sqlString = "insert into zppm_t_proginfo(z_procode,z_aud_ver,z_cap_ver,z_subj_cd,z_pgnam,z_prog_desc,z_time_estm,CREATE_TIMESTAMP) ";
			sqlString += "(select f_procode,f_aud_ver,f_cap_ver,f_subj_cd,'" + zppmTProginfo.getZPgnam() + "','" + zppmTProginfo.getZProgDesc() + "',f_subj_length,now() from t_channelzone where id='" + chanlZoneId + "')";
			genericDao.updateBySqlStr(sqlString);
		}
		return n;
	}
	public boolean getProgPreviewExistsByChanlid( String channelzoneID) {
		StringBuffer strb_sql = new StringBuffer();
		strb_sql.append("SELECT z_preview_exists\n" +
				"FROM zppm_t_proginfo AS pinfo\n" +
				"  INNER JOIN\n" +
				"\tt_channelzone AS zonelist ON zonelist.f_procode = pinfo.z_procode\n" +
				"\t\t\tAND zonelist.f_aud_Ver = pinfo.z_aud_ver\n" +
				"\t\t\tAND zonelist.f_cap_ver = pinfo.z_cap_ver\n" +
				"WHERE\n" +
				"    zonelist.id = ");
		strb_sql.append(channelzoneID);
		List<Map<String,?>> result=genericDao.executeSql(strb_sql.toString());
		if (result.isEmpty() == false) {
			Map resultmap = result.get(0);
			String previewExists = (String) resultmap.get("z_preview_exists");
			if (previewExists !=null && previewExists.equals("X")) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}
	public int updateProgPreview(String chanlZoneId,ZppmTProginfo zppmTProginfo,String[] filter){
		Condition condition=new Condition();
		condition.add("zProcode", zppmTProginfo.getZProcode());
		if(zppmTProginfo.getZAudVer() != null){
			condition.add("zAudVer", zppmTProginfo.getZAudVer());
		}
		if (zppmTProginfo.getZCapVer() != null) {
			condition.add("zCapVer", zppmTProginfo.getZCapVer());
		}

		int n=genericDao.updateByCondition(zppmTProginfo, filter,condition);
		if (n==0){
			String sqlString="insert into zppm_t_proginfo(z_procode,z_aud_ver,z_cap_ver,z_subj_cd,z_pgnam,z_prog_desc,z_preview_exists,z_time_estm,CREATE_TIMESTAMP) ";
			sqlString+="(select f_procode,f_aud_ver,f_cap_ver,f_subj_cd,null,null,'"+zppmTProginfo.getZPreviewExists()+"',f_subj_length,now() from t_channelzone where id='"+chanlZoneId+"')";
			genericDao.updateBySqlStr(sqlString);
		}
		return n;
	}

	public int removeChanlZoneProgs(Channelzone channelzone) {
		return genericDao.removeById(Channelzone.class, channelzone.getId());
	}
	public int updateReorder(String oldid,String newid){
		String sql="select concat(id,'')as id,concat(f_order,'') as f_order from t_channelzone where id in ("+oldid+","+newid+")";
		List<Map<String,?>> lst=genericDao.executeSql(sql);Map map=(Map)lst.get(0);String oldOrder="";String newOrder="";
		for (Map omap : lst) {
			String idString=(String)omap.get("id");
			if (idString.equals(oldid)){
				oldOrder=(String)omap.get("f_order");
			}else{
				newOrder=(String)omap.get("f_order");
			}
		}
		sql="update t_channelzone set f_order="+newOrder+" where id="+oldid;
		genericDao.updateBySqlStr(sql);
		sql="update t_channelzone set f_order="+oldOrder+" where id="+newid;
		genericDao.updateBySqlStr(sql);
		return 0;
	}
	public int updateChanlZoneProgs(Channelzone channelzone,String[] filter,Condition cond) {
		return genericDao.updateByCondition(channelzone, filter, cond);
	}
	public int updateChanlZoneOrder(Channelzone channelzone,String oldOrd) {
		if (StringUtils.isEmpty(channelzone.getChanlCd())){
			genericDao.update(channelzone, new String[]{"order"});
		}else{
			String sqlString="select * from t_channelzone where f_chanl_cd='"+channelzone.getChanlCd()+"' and f_order='"+channelzone.getOrder()+"'";
			List lst1=genericDao.executeSql(sqlString);
			if (lst1!=null&&lst1.size()>0){
				sqlString="";
				if (Integer.parseInt(oldOrd)<channelzone.getOrder().intValue()){//ie 从old2->new5
					sqlString="update t_channelzone set f_order=f_order-1 where f_chanl_cd='"+channelzone.getChanlCd()+"' and f_order>"+oldOrd+" and f_order<="+channelzone.getOrder();
				}else if (Integer.parseInt(oldOrd)>channelzone.getOrder().intValue()){//ie 从old5->new2
					sqlString="update t_channelzone set f_order=f_order+1 where f_chanl_cd='"+channelzone.getChanlCd()+"' and f_order>="+channelzone.getOrder()+" and f_order<"+oldOrd;
				}
				if (!"".equals(sqlString))
					genericDao.updateBySqlStr(sqlString);
			}
			genericDao.update(channelzone, new String[]{"order"});
		}
		return 1;//genericDao.update(channelzone, new String[]{"order"});
	}
	public List<Channelzone> getChanlZoneProgs(Channelzone channelzone) {
		Condition condition=new Condition();
		condition.add("chanlCd",channelzone.getChanlCd());
		condition.add("crtuser",channelzone.getCrtuser());
		return genericDao.getByCondition(Channelzone.class, condition, null);
	}
	public List getChanlZoneBeansList(Channelzone channelzone,int pageNo,int pageSize) {
		HashMap<String, String> hmapHashMap=new HashMap<String, String>();
		int firstRow = (pageNo-1) * pageSize;
		int lastRow = pageSize;
		hmapHashMap.put("firstRow", String.valueOf(firstRow));
		hmapHashMap.put("lastRow", String.valueOf(lastRow));
		hmapHashMap.put("chanlCd",channelzone.getChanlCd());
		hmapHashMap.put("crtuser",channelzone.getCrtuser());
		return genericDao.queryBySqlID("domain.multiTable.getChanlZoneList", hmapHashMap);
	}
	public String  getChanlZoneBeansListSize(Channelzone channelzone)
	{
		HashMap<String, String> hmapHashMap=new HashMap<String, String>();
		hmapHashMap.put("chanlCd", channelzone.getChanlCd());
		hmapHashMap.put("crtuser", channelzone.getCrtuser());
		List<String> lstList=genericDao.queryBySqlID("domain.multiTable.getChanlZoneListCnt", hmapHashMap);
		return (String)lstList.get(0);
	}
	/**
	 *
	 * @ps:获取t_contentlog中的源系统id
	 * @param
	 * @return
	 */
	public List<Map<String, ?>> getSourceId() {
		String sql = "select distinct Z_SOURCE_SYS_ID sysId from t_contentlog ";
		result = genericDao.executeSql(sql);
		return result;
	}

	/**
	 *
	 * @ps:获取t_contentlog中的目标系统id
	 * @return
	 */
	public List<Map<String, ?>> getTargetId() {
		String sql = "select distinct Z_TARGET_SYS_ID tarId from t_contentlog ";
		result = genericDao.executeSql(sql);
		return result;
	}

	/**
	 *
	 * @ps:获取t_contentlog中的目标系统的服务名称
	 * @return
	 */
	public List<Map<String, ?>> getTargetService() {
		String sql = "select distinct Z_TARGET_SERVICE tarSer from t_contentlog ";
		result = genericDao.executeSql(sql);
		return result;
	}
	/**
	 *
	 * @ps:
	 * @param map 保存参数信息的集合
	 * @return
	 */
	public List<Contentlog> getContentList(Map<String, String> map) {
		return genericDao.queryBySqlID("domain.ContentLog.getContentLogList", map);
	}

}